<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 27, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "access.UserGroup" );

/**
 * The user group list
 *
 */
class UserGroupList {
	
	/**
	 * The list of group
	 *
	 * @var array of UserGroup
	 */
	var $_groupList = array ();
	
	/**
	 * Add a group into UserGroupList
	 *
	 * @param UserGroup $userGroup
	 */
	function addGroup($userGroup) {
		$this->_groupList [$userGroup->code] = $userGroup;
	}
	
	/**
	 * Get a UserGroup by the group code.
	 * This function will get into the sub groupList of each group inside.
	 *
	 * @param string $groupCode
	 * 
	 * @return UserGroup
	 */
	function &getGroup($groupCode, $includeChildren = true) {
		$result = $this->_groupList [$groupCode];
		if (! empty ( $result )) {
			return $result;
		}
		
		if (! $includeChildren) {
			return null;
		}
		
		foreach ( $this->_groupList as $userGroup ) {
			$groupList = $userGroup->getChildrenGroups ();
			$result = &$groupList->getGroup ( $groupCode );
			if (! empty ( $result )) {
				return $result;
			}
		}
		return null;
	}
	
	/**
	 * Function for getting the group list of current UserGroupList
	 *
	 */
	function getGroupList() {
		return $this->_groupList;
	}
	
	/**
	 * Get all UserGroup that hold inside (included children groups)
	 *
	 * @return UserGroupList
	 */
	function getAllUserGroups() {
		$result = array ();
		$result = array_merge ( $result, $this->_groupList );
		
		foreach ( $this->_groupList as $userGroup ) {
			$result = array_merge ( $result, $userGroup->getAllUserGroups () );
		}
		return $result;
	}
	
	/**
	 * Check if this UserGroup can access a resource or not
	 *
	 * @param SiteResource $resource
	 */
	function canAccess($resource) {
		foreach ( $this->_groupList as $userGroup ) {
			if ($userGroup->canAccess ( $resource )) {
				return true;
			}
		}
		return false;
	}
}
?>